package cn.school.controller.classManage;


import cn.school.classManage.service.SchoolDormitoryService;
import cn.school.classManage.vo.SchoolDormitoryVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/dormitory")
@Api(tags = "School Dormitory=> 学校宿舍")
public class SchoolDormitoryController {

    @Autowired
    private SchoolDormitoryService schoolDormitoryService;

    @PostMapping("/findSchoolDormitoryPageList")
    @ApiOperation("findSchoolDormitoryPageList => 分页查询宿舍列表")
    public CommonResult<CommonPage<SchoolDormitoryVO>> findSchoolDormitoryPageList(@RequestBody SchoolDormitoryVO vo) throws ApiException, IOException {
        IPage<SchoolDormitoryVO> pageList = schoolDormitoryService.findSchoolDormitoryPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/addOrUpdateDormitory")
    @ApiOperation("addOrUpdateDormitory => 分页查询宿舍列表")
    public CommonResult<Integer> addOrUpdateDormitory(@RequestBody SchoolDormitoryVO vo) throws ApiException, IOException {
        int num = schoolDormitoryService.addOrUpdateDormitory(vo);
        if(num==-1){
            return CommonResult.failed("宿舍号重复");
        }
        if(num==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @PostMapping("/deleteDormitoryById")
    @ApiOperation("deleteDormitoryById => 删除宿舍信息")
    public CommonResult<CommonPage<SchoolDormitoryVO>> deleteDormitoryById(@RequestBody SchoolDormitoryVO vo) throws ApiException, IOException {
        int num = schoolDormitoryService.deleteDormitoryById(vo.getId());
        if(num==0){
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

}
